<?php
    $item_menu[0] = 5;
    $item_menu[1] = 2 ;
    $title = 'Productos';    

	if (!defined('URL')) { include('header.php'); }
    
	if (!isset($_SESSION["admin"])) {redirect("login.php");	exit(); }

	if(!empty($_SESSION['Msg'])) { echo mensaje_ok($_SESSION['Msg']);$_SESSION['Msg']='';	}
    
    
	$id = request('id',0);
	$familia_id = request('familia_id',0);
	$accion = request('accion','');

	
		$Familias = MenuFamilias( 0 );
        $select_familia = '<select name="familia_id" class="required input-xlarge">';
        foreach($Familias as $dd){
            if ($familia_id == $dd['id']) { $sel = "selected='selected'"; } else {$sel='';}
			if( !empty($dd['child'])) {
				$select_familia.= "<optgroup label='{$dd['nombre']}'>";
				foreach($dd['child'] as $cc){
					if ($familia_id == $cc['id']) { $sel = "selected='selected'"; } else {$sel='';}
					$select_familia.= "<option value='{$cc['id']}' $sel >$separador {$cc['nombre']}</option>";
				}
				$select_familia.= "</optgroup>";
			} else {
				$select_familia.= "<option value='{$dd['id']}' $sel >$separador {$dd['nombre']}</option>";
			}
		}
        $select_familia.= "</select>";
	

	if($accion=='guardar'){

		$data = $_POST;
		unset($data['accion']);
		unset($data['submit']);
	
		$ok = $db->Replace('productos', $data,'id', $autoquote = true); 
		
	
		if ($ok) { $_SESSION['Msg'] = 'El Producto se elimin&oacute; correctamente.';
		} else { $_SESSION['Msg'] = $cond."<hr>".'ERROR!! No se pudo eliminar el producto.';	}
		redirect("productos.php?accion=listar&familia_id{$data['familia_id']}");
		die();


	}

	include_once('productos_nuevo.html.php');
	die();
?>
